package com.pb.b2b.bean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.pb.b2b.model.Language;
import com.pb.b2b.model.Mark;
import com.pb.b2b.model.Product;
import com.pb.b2b.model.ProductLanguage;
import com.pb.b2b.model.enums.Status;
import com.pb.b2b.service.CategoryLanguageService;
import com.pb.b2b.service.CategoryService;
import com.pb.b2b.service.LanguageService;
import com.pb.b2b.service.MarkService;

@Component
@Scope("view")
@ManagedBean(name = "createProductBean")
public class CreateProductBean extends CategoryBaseBean {
	private static final long serialVersionUID = 1L;

	@Autowired
	LanguageService languageService;

	@Autowired
	CategoryService categoryService;

	@Autowired
	CategoryLanguageService categoryLanguageService;

	@Autowired
	MarkService markService;

	private Product newProduct;
	private List<ProductLanguage> newProductLanguages;

	private List<Language> languageList;

	private TreeNode[] selectedNodes;
	private List<SelectItem> marks;
	private String selectedMark;

	@PostConstruct
	public void init() {
		defaultCategoryLanguages = categoryLanguageService.getDefaultCategoryLanguages();
		if (hasError()) {
			return;
		}

		languageList = languageService.getLanguages(Status.AKTIVE);
		if (hasError()) {
			return;
		}

		List<Mark> markList = markService.getMarks();
		initMarkCombo(markList);

		newProduct = new Product();
		newProductLanguages = new ArrayList<ProductLanguage>();
		for (Language language : languageList) {
			ProductLanguage productLanguage = new ProductLanguage();
			productLanguage.setLanguage(language);
			productLanguage.setProduct(newProduct);
			newProductLanguages.add(productLanguage);
		}
		showRootCategory = false;
		treeInit();
	}

	/* METHOD */

	private void initMarkCombo(List<Mark> list) {
		marks = new ArrayList<SelectItem>();

		for (Mark mark : list) {
			marks.add(new SelectItem(mark.getId(), mark.getName()));
		}
	}

	@Override
	public String confirmAction() {
		// // Language language = new Language();
		// // language.setName(name);
		// // language.setCode(code);
		// // languageService.save(language);
		// // languageList = languageService.getAllLanguages();
		// if (selectedNode == null) {
		// FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
		// "VALIDATION_KATEGORI_SECILMELI");
		// return RETURN_ERROR;
		// }
		//
		// for (CategoryLanguage categoryLanguage : newCategoryLanguages) {
		// if (StringUtils.isEmpty(categoryLanguage.getName())) {
		// FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
		// "ERROR_CATEGORY_NAME_EMPTY",
		// new Object[] { categoryLanguage.getLanguage().getName() });
		// return RETURN_ERROR;
		// }
		// if (StringUtils.isEmpty(categoryLanguage.getDescription())) {
		// FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
		// "ERROR_CATEGORY_DESCRIPTION_EMPTY",
		// new Object[] { categoryLanguage.getLanguage().getName() });
		// return RETURN_ERROR;
		// }
		// }
		//
		// Category parentCategory = ((CategoryLanguage)
		// selectedNode.getData()).getCategory();
		//
		// newCategory.setStatus(true);
		// if (parentCategory != null && parentCategory.getId() != null)
		// newCategory.setParentCategory(parentCategory);
		//
		// newCategory.setCategoryLanguages(new
		// HashSet<CategoryLanguage>(newCategoryLanguages));

		return RETURN_SUCCESS;
	}

	@Override
	public String executeAction() {
		// categoryService.save(newCategory);
		// if (hasError()) {
		// return RETURN_ERROR;
		// }
		return RETURN_SUCCESS;
	}

	public final TreeNode[] getSelectedNodes() {
		return selectedNodes;
	}

	public final void setSelectedNodes(TreeNode[] selectedNodes) {
		this.selectedNodes = selectedNodes;
	}

	public final List<ProductLanguage> getNewProductLanguages() {
		return newProductLanguages;
	}

	public final void setNewProductLanguages(List<ProductLanguage> newProductLanguages) {
		this.newProductLanguages = newProductLanguages;
	}

	public final Product getNewProduct() {
		return newProduct;
	}

	public final void setNewProduct(Product newProduct) {
		this.newProduct = newProduct;
	}

	public final List<SelectItem> getMarks() {
		return marks;
	}

	public final void setMarks(List<SelectItem> marks) {
		this.marks = marks;
	}

	public final String getSelectedMark() {
		return selectedMark;
	}

	public final void setSelectedMark(String selectedMark) {
		this.selectedMark = selectedMark;
	}

	/* GETTER _ SETTER */

}
